<template>
  <div class="list-table">
    <div class="area-header">
      <span class="area-header-title">{{ $t('项目费用') }}</span>
    </div>
    <el-form ref="queryForm" :model="queryParams" :inline="true">
      <el-form-item :label="$t('项目阶段')" prop="projectStage">
        <el-select v-model="queryParams.projectStage" :placeholder="$t('项目阶段')" clearable>
          <el-option
            v-for="dict in projectStageOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item :label="$t('费用类型')" prop="amountType">
        <el-select v-model="queryParams.amountType" :placeholder="$t('费用类型')" clearable>
          <el-option
            v-for="dict in amountTypeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="queryHandle">{{ $t('搜索') }}</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{ $t('重置') }}</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['deal:expense:edit']"
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="addHandle"
        >{{ $t('新增') }}</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['deal:expense:import']"
          type="info"
          icon="el-icon-upload2"
          size="mini"
          @click="handleImport"
        >{{ $t('批量导入') }}
        </el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="dealBusinessExpenseList" border tooltip-effect="darkTable">
      <el-table-column :label="$t('序号')" type="index" align="center" show-overflow-tooltip width="50">
        <template slot-scope="scope">
          {{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column :label="$t('发生日期')" align="center" prop="occurrenceDate" width="180">
        <template slot-scope="scope">
          <span><el-link class="link-a" type="primary" @click="updateHandle(scope.row)">{{ parseTime(scope.row.occurrenceDate) }}</el-link></span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('项目阶段')" align="center" prop="projectStage" :formatter="projectStageFormat" />
      <el-table-column :label="$t('费用类型')" align="center" prop="amountType" :formatter="amountTypeFormat" />
      <el-table-column :label="$t('费用金额(元)')" align="center" prop="expenses" width="180">
        <template slot-scope="scope">
          <span class="money-text">{{ moneyFormat(scope.row.expenses) }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('费用说明')" align="center" prop="feeDescription" />
      <el-table-column :label="$t('操作')" align="center" class-name="small-padding fixed-width" width="200">
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['deal:expense:edit']"
            size="small"
            type="text"
            icon="el-icon-edit"
            @click="updateHandle(scope.row)"
          >{{ $t('修改') }}</el-button>
          <el-button
            v-hasPermi="['deal:expense:remove']"
            size="small"
            type="text"
            icon="el-icon-delete"
            @click="deleteHandle(scope.row)"
          >{{ $t('删除') }}</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改持有型物业-项目费用对话框 -->
    <el-dialog :title="$t(title)" :visible.sync="formOpen" :close-on-click-modal="false" :lock-scroll="false" custom-class="dialogForm" append-to-body>
      <dealBusinessExpenseForm ref="dealBusinessExpenseForm" />
    </el-dialog>

    <!-- 导入 -->
    <el-dialog
      :title="$t('项目费用导入')"
      :visible.sync="upload.open"
      width="400px"
      :close-on-click-modal="false"
      :lock-scroll="false"
    >
      <el-upload
        ref="upload"
        :limit="1"
        accept=".xlsx, .xls"
        :headers="upload.headers"
        :action="upload.url + '?updateSupport=' + upload.updateSupport+ '&dealId=' + upload.dealId"
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
        drag
      >
        <i class="el-icon-upload" />
        <div class="el-upload__text">
          {{ $t('将文件拖到此处，或') }}
          <em>{{ $t('上传') }}</em>
        </div>
        <!--        <div slot="tip" class="el-upload__tip">-->
        <!--          <el-checkbox v-model="upload.updateSupport" /> {{ $t('是否更新已经存在的数据') }}-->
        <!--        </div>-->
        <div slot="tip" class="el-upload__tip" style="color:red">
          {{ $t('提示：仅允许导入“xls”或“xlsx”格式文件！') }}
          <el-link type="primary" style="font-size:12px" @click="fetchTemplate">
            {{ $t('下载模板') }}
          </el-link>
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button @click="upload.open = false">{{ $t("取消") }}</el-button>
        <el-button type="primary" @click="submitFileForm">{{ $t("确定") }}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listDealBusinessExpense, delDealBusinessExpense } from '@/api/deal/expense/dealBusinessExpense'
import dealBusinessExpenseForm from './dealBusinessExpenseForm.vue'
import { getToken } from '@/utils/auth'
export default {
    components: {
        dealBusinessExpenseForm
    },
  data() {
      return {
          // 遮罩层
          loading: true,
          // 总条数
          total: 0,
          // 持有型物业-项目费用表格数据
          dealBusinessExpenseList: [],
          // 弹出层标题
          title: '项目费用',
          // 是否显示弹出层
          formOpen: false,
        // 项目阶段字典
        projectStageOptions: [],

        // 费用类型字典
        amountTypeOptions: [],

          // 查询参数
          queryParams: {
              dealId: this.$route.query.id,
              projectStage: undefined,
              amountType: undefined,
              pageNum: 1,
              pageSize: 10
          },
          // 表单参数
          form: {},
          // 表单校验
          rules: {},
          // 用户导入参数
          upload: {
            // 是否显示弹出层（用户导入）
            open: false,
            // 弹出层标题（用户导入）
            title: '',
            // 是否禁用上传
            isUploading: false,
            // 是否更新已经存在的用户数据
            updateSupport: 0,
            // dealId
            dealId: this.$route.query.id,
            // 设置上传的请求头部
            headers: { Authorization: 'Bearer ' + getToken() },
            // 上传的地址
            url: process.env.VUE_APP_BASE_API + '/expense/dealBusinessExpense/importData'
          }
      }
  },
  created() {
      this.getList()
      this.getDicts('deal_stage_of_project').then(response => {
          this.projectStageOptions = response.data
      })
      this.getDicts('deal_amount_type').then(response => {
          this.amountTypeOptions = response.data
      })
  },
  methods: {
    /** 查询持有型物业-项目费用列表 */
    getList() {
        this.loading = true
        listDealBusinessExpense(this.queryParams).then(response => {
            this.dealBusinessExpenseList = response.rows
            this.total = response.total
            this.loading = false
        }).catch(() => {
            this.loading = false
        })
    },
    // 项目阶段字典翻译
    projectStageFormat(row, column) {
      console.log(column)
      return this.selectDictLabel(this.projectStageOptions, row.projectStage)
    },
    // 费用类型字典翻译
    amountTypeFormat(row, column) {
      console.log(column)
      return this.selectDictLabel(this.amountTypeOptions, row.amountType)
    },

    // 表单重置
    reset() {
        this.form = {
            id: undefined,
            dealId: this.$route.query.id,
            occurrenceDate: undefined,
            projectStage: undefined,
            amountType: undefined,
            expenses: undefined,
            feeDescription: undefined,
            remark: undefined,
            createBy: undefined,
            createTime: undefined,
            updateBy: undefined,
            updateTime: undefined,
            delFlag: undefined
        }
        this.resetForm('form')
    },
    /** 搜索按钮操作 */
    queryHandle() {
        this.queryParams.pageNum = 1
        this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
        this.resetForm('queryForm')
        this.queryHandle()
    },
    /** 新增按钮操作 */
    addHandle() {
        this.formOpen = true
        this.$nextTick(() => {
            this.$refs.dealBusinessExpenseForm.init(null)
        })
    },
    /** 修改按钮操作 */
    updateHandle(row) {
        this.formOpen = true
        this.$nextTick(() => {
            // 调用form页面的 init 方法初始化数据
            this.$refs.dealBusinessExpenseForm.init(row)
        })
    },
    /** 删除按钮操作 */
    deleteHandle(row) {
        this.$confirm(this.$t('是否确认删除?'), this.$t('警告'), {
            confirmButtonText: this.$t('确定'),
            cancelButtonText: this.$t('取消'),
            type: 'warning'
        }).then(() => {
            return delDealBusinessExpense(row.id)
        }).then(() => {
            this.getList()
            this.msgSuccess(this.$t('删除成功'))
        }).catch(() => {
            this.loading = false
        })
    },
    /** 导入按钮操作 */
    handleImport() {
      this.upload.title = this.$t('导入')
      this.upload.open = true
    },
    // 文件上传中处理
    handleFileUploadProgress() {
      this.upload.isUploading = true
    },
    // 文件上传成功处理
    handleFileSuccess(response) {
      this.upload.open = false
      this.upload.isUploading = false
      this.$refs.upload.clearFiles()
      this.$alert(response.msg, this.$t('导入结果'), { dangerouslyUseHTMLString: true })
      this.getList()
    },
    // 下载模板
    fetchTemplate() {
      const href1 = window.location.href
      const href2 = this.$route.path
      const url = href1.substring(0, href1.indexOf(href2))
      window.location.href = url + '/template/dealBusinessExpenseImportTemplate.xlsx'
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit()
    }
  }
}
</script>
